/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.MachineManagement;

import Beans.Requests.MachineManagement.Components.NetworkAdapterLiveSettings;
import Beans.Requests.MachineManagement.Components.SharedFolderSettings;
import Utilities.PermissionsConstants;
import java.util.ArrayList;

/**
 * Instances of this class are used by the user interface when a request to
 * modify the live machine settings is sent to the machines management service.
 *
 * @author Angel Sanadinov
 */
public class ModifyMachineLiveSettingsRequest extends MachineManagementRequest
{
    private ArrayList<SharedFolderSettings> newSharedFolders;   //shared folder settings: create
    private ArrayList<String> sharedFoldersForRemoval;          //shared folder settings: remove
    private NetworkAdapterLiveSettings[] networksSettings;      //live networking settings

    /**
     * No-argument constructor. <br><br>
     * All fields are set to their invalid values.
     */
    public ModifyMachineLiveSettingsRequest()
    {
        this.newSharedFolders = null;
        this.sharedFoldersForRemoval = null;
        this.networksSettings = null;
        setRequestedAction(PermissionsConstants.ACTION_MACHINE_MODIFY_LIVE);
    }

    /**
     * Constructs an incomplete request for modifying the live settings of a
     * virtual machine. <br><br>
     *
     * <b>Note:</b> <i>All data needs to be explicitly set!</i>
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server on which the machine resides
     * @param machineId the id of the machine
     */
    public ModifyMachineLiveSettingsRequest(int requestorId, int serverId, String machineId)
    {
        this();
        setRequestorId(requestorId);
        setServerId(serverId);
        setMachineId(machineId);
        setRequestedAction(PermissionsConstants.ACTION_MACHINE_MODIFY_LIVE);
    }

    /**
     * Adds a new shared folder to the virtual machine.
     *
     * @param folder shared folder settings
     */
    public void addNewSharedFolder(SharedFolderSettings folder)
    {
        if(newSharedFolders == null)
            newSharedFolders = new ArrayList<SharedFolderSettings>();
        else
            ;

        newSharedFolders.add(folder);
    }

    /**
     * Removes a shared folder from the virtual machine.
     *
     * @param folderName the name of the shared folder to be removed
     */
    public void addSharedFolderForRemoval(String folderName)
    {
        if(sharedFoldersForRemoval == null)
            sharedFoldersForRemoval = new ArrayList<String>();
        else
            ;
        
        sharedFoldersForRemoval.add(folderName);
    }

    /**
     * Sets the live networking settings for the machine (for each network adapter).
     *
     * @param networksSettings the networking settings
     */
    public void setNetworksSettings(NetworkAdapterLiveSettings[] networksSettings)
    {
        this.networksSettings = networksSettings;
    }

    /**
     * Retrieves the shared folders to be added to the machine.
     *
     * @return the shared folders settings, if available or <code>null</code> otherwise
     */
    public ArrayList<SharedFolderSettings> getNewSharedFolders() {

        return newSharedFolders;
    }

    /**
     * Retrieves the shared folders to be removed from the machine.
     *
     * @return the shared folders settings, if available or <code>null</code> otherwise
     */
    public ArrayList<String> getSharedFoldersForRemoval()
    {
        return sharedFoldersForRemoval;
    }

    /**
     * Retrieves the live networking settings of the machine (for each network adapter).
     *
     * @return the networking settings, if available or <code>null</code> otherwise
     */
    public NetworkAdapterLiveSettings[] getNetworksSettings()
    {
        return networksSettings;
    }

    /**
     * Checks if the networking settings are valid.
     *
     * @return <code>true</code> if the settings are valid or <code>false</code> otherwise
     */
    private boolean areNetworksSettingsValid()
    {
        if(networksSettings != null)
        {
            for(NetworkAdapterLiveSettings adapterSettings : networksSettings)
            {
                if(adapterSettings != null && !adapterSettings.isValid())
                    return false;
                else
                    ;
            }
        }
        else
            ;

        return true;
    }

    /**
     * Checks if the shared folders settings are valid. <br><br>
     *
     * <b>Note:</b> <i>If the shared folder settings are set to <code>null</code>
     * they are still considered valid.</i>
     *
     * @return <code>true</code> if the settings are valid or <code>false</code> otherwise
     */
    private boolean areSharedFoldersSettingsValid()
    {
        if(newSharedFolders != null)
        {
            for(SharedFolderSettings folderSettings : newSharedFolders)
            {
                if(folderSettings != null && !folderSettings.isValid())
                    return false;
                else
                    ;
            }
        }
        else
            ;

        return true;
    }

    @Override
    public boolean isValid()
    {
        if(super.isValid() && areSharedFoldersSettingsValid() && areNetworksSettingsValid())
            return true;
        else
            return false;
    }

    /**
     * Returns a textual representation of the object. <br>
     *
     * It is in the form: "ClassName: (machineId)".
     *
     * @return the object represented as a String
     */
    @Override
    public String toString()
    {
        return ModifyMachineLiveSettingsRequest.class.getSimpleName()
                + ": (" + getMachineId() + ")";
    }
}
